package com.cloud.consumer.service;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Service
public class HelloService {

	private final Logger logger = Logger.getLogger(getClass());

	@Autowired
	private RestTemplate restTemplate;

	@HystrixCommand(fallbackMethod = "helloFallBack", commandKey = "helloKey")
	public String helloservice() {
		Long start = System.currentTimeMillis();

		String result = restTemplate.getForEntity("http://hello-service/hello", String.class).getBody();

		Long end = System.currentTimeMillis();

		logger.info("spend time :" + (end - start));
		return result;

	}

	public String helloFallBack(Throwable e) {
		return "error:" + e;
	}
}
